package data

import (
	"context"
	"github.com/go-kratos/kratos/v2/log"
	"gorm.io/gorm"
	"phm/internal/biz"
	"phm/models/generator/model"
	"phm/models/generator/query"
	"time"
)

type timedTaskFaultResultRepo struct {
	Db  biz.DataRepo
	log *log.Helper
}

func NewTimedTaskFaultResultRepo(data biz.DataRepo, logger log.Logger) biz.TimedTaskFaultResultRepo {
	return &timedTaskFaultResultRepo{Db: data, log: log.NewHelper(logger)}
}

func (u *timedTaskFaultResultRepo) CreateTimedTaskFaultResult(ctx context.Context, uModel *model.TimedTaskFaultResult, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).TimedTaskFaultResult
	err := opt.WithContext(ctx).Create(uModel)
	if err != nil {
		return err
	}
	return nil
}

func (u *timedTaskFaultResultRepo) UpdateTimedTaskFaultResult(ctx context.Context, uModel *model.TimedTaskFaultResult, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).TimedTaskFaultResult
	info, err := opt.WithContext(ctx).Where(opt.ID.Eq(uModel.ID)).Updates(uModel)
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("更新错误：%v", err)
		return err
	}
	return nil
}

func (u *timedTaskFaultResultRepo) DeleteTimedTaskFaultResultByIds(ctx context.Context, ids []string, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	role := query.Use(s).TimedTaskFaultResult
	info, err := role.WithContext(ctx).Where(role.ID.In(ids...)).Delete()
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("删除错误：%v", ids)
		return err
	}
	return nil
}

func (u *timedTaskFaultResultRepo) GetTimedTaskFaultResultById(ctx context.Context, id string) (*model.TimedTaskFaultResult, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).TimedTaskFaultResult
	user, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0), opt.ID.Eq(id)).First()
	if err != nil {
		return nil, err
	}
	return user, nil
}

func (u *timedTaskFaultResultRepo) GetTimedTaskFaultResultByRadarIdList(ctx context.Context, idList []string) (map[string]*biz.RadarFaultResult, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).TimedTaskFaultResult
	res := []biz.RadarFaultResult{}
	// 10分钟前的time
	tenMinuteBefore := time.Now().Add(-10 * time.Minute)
	err := opt.WithContext(ctx).
		Select(opt.RadarID,
			opt.NonSwitchLow.Sum().As("non_switch_low"),
			opt.NonSwitchFault.Sum().As("non_switch_fault"),
			opt.SwitchFault.Sum().As("switch_fault")).
		Where(opt.IsSoftDelete.Eq(0), opt.RadarID.In(idList...), opt.CreateDateTime.Lte(time.Now()), opt.CreateDateTime.Gt(tenMinuteBefore)).
		Group(opt.RadarID).Scan(&res)
	if err != nil {
		return nil, err
	}
	resMap := make(map[string]*biz.RadarFaultResult)
	if res == nil || len(res) == 0 {
		return resMap, nil
	}
	for _, v := range res {
		resMap[v.RadarId] = &v
	}
	return resMap, nil
}

func (u *timedTaskFaultResultRepo) GetTimedTaskFaultResultByIds(ctx context.Context, ids []string) ([]*model.TimedTaskFaultResult, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).TimedTaskFaultResult
	users, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0), opt.ID.In(ids...)).Find()
	if err != nil {
		return nil, err
	}
	return users, nil
}

func (u *timedTaskFaultResultRepo) GetTimedTaskFaultResultList(ctx context.Context, queryParam string, pageNum, pageSize int64) ([]*model.TimedTaskFaultResult, int64, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).TimedTaskFaultResult
	if pageNum == 0 && pageSize == 0 {
		radars, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0)).Find()
		if err != nil {
			return nil, 0, err
		}
		return radars, 0, nil
	}
	list, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0)).Limit(int(pageSize)).Offset(int((pageNum - 1) * pageSize)).Find()
	if err != nil {
		return nil, 0, err
	}
	// 计算总数
	count, _ := opt.WithContext(ctx).Count()
	return list, count, nil
}

func (u *timedTaskFaultResultRepo) GetDB() *gorm.DB {
	return u.Db.GetDB()
}
